package com.gears_software.sxdc.dccx.ui.activity;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IVehicleFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.finder.IBaseFinder;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.TypeModel;
import com.gears_software.sxdc.dccx.data.model.VehicleModel;
import com.gears_software.sxdc.dccx.data.model.request.RAddVehicleModel;
import com.gears_software.sxdc.dccx.ui.util.ShowSeat;
import com.orhanobut.logger.Logger;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/7/25.
 */

@EActivity(R.layout.activity_add_carinfo)
public class AddCarInfoActivity extends AppCompatActivity {

    @App
    DCCXApplication app;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView txt_right_icon;
    @ViewById
    TextView txt_right_icon1;
    @ViewById
    TextView txt_color;
    @ViewById
    TextView txt_count;
    @ViewById
    EditText edit_carinfo;
    @ViewById
    EditText edit_carno;
    @ViewById
    Button btn_addcar;
    @Extra
    String title;
    @Extra
    String flag;
    @Extra
    VehicleModel vehicleModel;

    List<TypeModel> optionsColorItems = new ArrayList<TypeModel>();
    List<String> optionsCountItems = new ArrayList<String>();
    OptionsPickerView pickerViewColor;
    OptionsPickerView pickerViewCount;

    @Bean
    GearsErrorHandler errorHandler;
    @RestService
    IVehicleFinder vehicleFinder;
    @RestService
    IBaseFinder baseFinder;
    ShowSeat showSeat = new ShowSeat();


    @AfterInject
    void inject() {
        vehicleFinder.setRestErrorHandler(errorHandler);
        baseFinder.setRestErrorHandler(errorHandler);
    }

    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        showSeat.initSeat(this, txt_count);
        if (flag.equals("modify")) {
            btn_addcar.setText("修改车辆");
            if (vehicleModel != null) {
                edit_carinfo.setText(vehicleModel.getCarmodel());
                edit_carno.setText(vehicleModel.getVehicleno());
                txt_color.setText(vehicleModel.getVehiclecolor());
                txt_count.setText(vehicleModel.getSeats());
            }
        }

    }

    void initIconFont() {
        actionbar_title.setText(title);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
        txt_right_icon.setTypeface(app.iconfont);
        txt_right_icon1.setTypeface(app.iconfont);
    }


    @Click
    void btn_addcar() {
        if (edit_carinfo.getText().toString().equals("")) {
            TastyToast.makeText(this, "车辆信息不得为空", app.SHORT, app.ERROR);
        } else if (edit_carno.getText().toString().equals("")) {
            TastyToast.makeText(this, "车牌号码不得为空", app.SHORT, app.WARNING);
        } else if (edit_carno.getText().toString().length() != 7) {
            TastyToast.makeText(this, "请填写正确的车牌号码", app.SHORT, app.WARNING);
        } else if (txt_color.getText().toString().equals("请选择车辆颜色")) {
            TastyToast.makeText(this, "请选择车辆颜色", app.SHORT, app.WARNING);
        } else if (txt_count.getText().toString().equals("请选择座位数")) {
            TastyToast.makeText(this, "请选择座位数", app.SHORT, app.WARNING);
        } else {
            if (flag.equals("add")) {
                RAddVehicleModel model = new RAddVehicleModel();
                model.setCarmodel(edit_carinfo.getText().toString().trim().toUpperCase());
                model.setVehicleno(edit_carno.getText().toString().trim().toUpperCase());
                model.setVehiclecolor(txt_color.getText().toString());
                model.setCaruse(2);
                model.setSeats(txt_count.getText().toString());
                addCarInfo(model);
            } else if (flag.equals("modify")) {
                RAddVehicleModel RAddVehicleModel = new RAddVehicleModel();
                RAddVehicleModel.setCarmodel(edit_carinfo.getText().toString().trim().toUpperCase());
                RAddVehicleModel.setVehicleno(edit_carno.getText().toString().trim().toUpperCase());
                RAddVehicleModel.setVehiclecolor(txt_color.getText().toString());
                RAddVehicleModel.setCaruse(2);
                RAddVehicleModel.setSeats(txt_count.getText().toString());
                updateCarInfo(RAddVehicleModel, vehicleModel.getId());
            }

        }
    }

    @Background
    void addCarInfo(RAddVehicleModel model) {
        ResultModel<VehicleModel> result = vehicleFinder.add(model);
        showResult(result);
    }

    @UiThread
    void showResult(ResultModel<VehicleModel> result) {
        if (result != null) {
            if (result.getErrno().equals("0")) {
                TastyToast.makeText(this, "添加车辆信息成功", app.SHORT, app.SUCCESS);
                setResult(1);
                this.finish();
            } else {
                TastyToast.makeText(this, "添加车辆信息失败", app.SHORT, app.ERROR);
            }
        }

    }

    @Background
    void updateCarInfo(RAddVehicleModel model, String id) {
        ResultModel<String> res = vehicleFinder.updateVehicle(model, id);
        showUpdateResult(res);
    }

    @UiThread
    void showUpdateResult(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                TastyToast.makeText(this, "修改车辆信息成功", app.SHORT, app.SUCCESS);
                setResult(1);
                this.finish();
            } else {
                TastyToast.makeText(this, "修改车辆信息失败", app.SHORT, app.ERROR);
            }
        }
    }

    @Click
    void back_icon() {
        this.finish();
    }

    @Click
    void re_carcolor() {
        getVehicleColor();
    }

    @Background
    void getVehicleColor() {
        ResultModel<List<TypeModel>> res = baseFinder.getVehicleColor();
        Logger.d(res);
        showColorResult(res);
    }

    @UiThread
    void showColorResult(ResultModel<List<TypeModel>> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                optionsColorItems = res.getData();
                Logger.d(optionsColorItems);
                if (optionsColorItems != null && optionsColorItems.size() > 0) {
                    closeKeyboard();
                    pickerViewColor = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int options2, int options3, View v) {
                            txt_color.setText(optionsColorItems.get(options1).getTypename());
                        }
                    }).build();
                    pickerViewColor.setPicker(optionsColorItems);
                    pickerViewColor.show();
                }
            }
        }
    }

    private void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Click
    void re_carcount() {
        closeKeyboard();
        showSeat.showSeat();
    }
}
